package news.web;

import com.github.pagehelper.PageInfo;
import news.model.Admin;
import news.model.CommonResult;
import news.model.Menu;
import news.model.Role;
import news.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(path = "/role/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @RequestMapping(path = "/roles")
    public CommonResult roles(@RequestBody Role role){

        PageInfo<Role> roles = roleService.roleList(role);
        roles.getList();
        roles.getTotal();
        CommonResult commonResult = new CommonResult(200,roles,"查询成功");
        return commonResult;
    }

    @GetMapping(path = "/menus")
    public CommonResult roles(){
        List<Menu> menuList = roleService.menus();
        CommonResult commonResult = new CommonResult(200,menuList,"增加成功");
        return commonResult;
    }

    @PostMapping(path = "/save")
    public CommonResult save(@RequestBody Role role, @RequestHeader String adminToken){
        if(role.getId() == null){
            roleService.save(role,adminToken);
        }else{
            roleService.update(role,adminToken);
        }
        CommonResult commonResult = new CommonResult(200,"保存成功");
        return commonResult;
    }

    @GetMapping(path = "/delete")
    public CommonResult delete(Integer id){
        roleService.delete(id);
        CommonResult commonResult = new CommonResult(200,"删除成功");
        return commonResult;
    }

    @GetMapping(path = "/findRoleById")
    public CommonResult findRoleById(Integer id){
        Role role = roleService.findRoleById(id);
        CommonResult commonResult = new CommonResult(200,role,"查询成功");
        return commonResult;
    }

}
